<?php

namespace app\common\logic;

include_once EXTEND_PATH . 'aliyun/aliyun-php-sdk-core/Config.php';

use Alidns\Request\V20150109 as dns;
use DefaultAcsClient;
use DefaultProfile;
use mikkle\tp_wechat\support\Curl;
use think\Cache;
use think\Db;
use think\Exception;
use vod\Request\V20170321 as vod;

class VodLogic extends Logic
{
    protected static $regionId = 'cn-shanghai'; // 点播服务所在的Region，国内请填cn-shanghai，不要填写别的区域

    protected static function init_vod_client()
    {
        $config = get_addon_config('alioss');
//        log_print($config);
        $profile = DefaultProfile::getProfile(self::$regionId, $config['app_id'],
            $config['app_key']);
        return new DefaultAcsClient($profile);
    }

    /**
     * 创建视频信息
     * @param array $param
     * @return mixed|\SimpleXMLElement
     */
    static function create_upload_video($param = [])
    {
        $title = isset($param['title']) ? $param['title'] : '';
        $srcFile = isset($param['srcFile']) ? $param['srcFile'] : '';
        $desc = isset($param['desc']) ? $param['desc'] : '';
        $pic = isset($param['pic']) ? $param['pic'] : '';
        $ip = isset($param['ip']) ? $param['ip'] : '';
        $tags = isset($param['tags']) ? $param['tags'] : '';

        $client = self::init_vod_client();
        $request = new vod\CreateUploadVideoRequest();
        //视频源文件标题(必选)
        $request->setTitle($title);
        //视频源文件名称，必须包含扩展名(必选)
        $request->setFileName($srcFile);
        //视频源文件字节数(可选)
        $request->setFileSize(0);
        //视频源文件描述(可选)
        $request->setDescription($desc);
        //自定义视频封面URL地址(可选)
        $request->setCoverURL($pic);
        //上传所在区域IP地址(可选)
        $request->setIP($ip);
        //视频标签，多个用逗号分隔(可选)
        $request->setTags($tags);
        //视频分类ID(可选)
        $request->setCateId(0);
        $response = $client->getAcsResponse($request);
        return $response;
    }

    /**
     * 刷新视频凭证
     * @param $videoId
     * @return mixed|\SimpleXMLElement
     */
    static function refresh_upload_video($videoId)
    {
        $client = self::init_vod_client();
        $request = new vod\RefreshUploadVideoRequest();
        //视频ID(必选)
        $request->setVideoId($videoId);
        $request->setAcceptFormat('JSON');
        $response = $client->getAcsResponse($request);
        return $response;
    }

    /**
     * 创建视频信息
     * @param $imageType
     * @param $imageExt
     * @return mixed|\SimpleXMLElement
     */
    static function create_upload_image($imageType, $imageExt)
    {
        $client = self::init_vod_client();
        $request = new vod\CreateUploadImageRequest();
        $request->setImageType($imageType);
        $request->setImageExt($imageExt);
        $request->setAcceptFormat('JSON');
        return $client->getAcsResponse($request);
    }

    /**
     * 获取播放视频凭证
     * @param $VideoId
     * @return mixed|\SimpleXMLElement
     */
    static function get_video_playAuth($VideoId)
    {
        try {
            $client = self::init_vod_client();

            $request = new vod\GetVideoPlayAuthRequest();
            $request->setAcceptFormat('JSON');
            $request->setRegionId(self::$regionId);
            $request->setVideoId($VideoId);            //视频ID
            $response = $client->getAcsResponse($request);
            return $response;
        } catch (Exception $e) {
            trace($e->getMessage(), 'error');
            return null;
        }
    }

    static function get_video_info($videoId)
    {
        try {
            $client = self::init_vod_client();

            $request = new vod\GetVideoInfoRequest();
            $request->setVideoId($videoId);
            $request->setAcceptFormat('JSON');
            return $client->getAcsResponse($request);
        } catch (Exception $e) {
            trace($e->getMessage(), 'error');
            return null;
        }
    }

    static function get_play_info($videoId)
    {
        try {
            $client = self::init_vod_client();

            $request = new vod\GetPlayInfoRequest();
            $request->setVideoId($videoId);
            $request->setAuthTimeout(3600);
            $request->setAcceptFormat('JSON');
            return $client->getAcsResponse($request);
        } catch (Exception $e) {
            trace($e->getMessage(), 'error');
            return null;
        }
    }

    /**
     * 获取视频信息
     * @param $start_time
     * @param $end_time
     * @return mixed|\SimpleXMLElement
     */
    static function get_video_list($start_time, $end_time)
    {
        $client = self::init_vod_client();

        $request = new vod\GetVideoListRequest();
        // 示例：分别取一个月前、当前时间的UTC时间作为筛选视频列表的起止时间
        $localTimeZone = date_default_timezone_get();
        date_default_timezone_set('UTC');
        $utcNow = gmdate('Y-m-d\TH:i:s\Z', $end_time);
        $utcMonthAgo = gmdate('Y-m-d\TH:i:s\Z', $start_time);
        date_default_timezone_set($localTimeZone);
        $request->setStartTime($utcMonthAgo);   // 视频创建的起始时间，为UTC格式
        $request->setEndTime($utcNow);          // 视频创建的结束时间，为UTC格式
        #$request->setStatus('Uploading,Normal,Transcoding');  // 视频状态，默认获取所有状态的视频，多个用逗号分隔
        #$request->setCateId(0);               // 按分类进行筛选
        $request->setPageNo(1);
        $request->setPageSize(20);
        $request->setAcceptFormat('JSON');
        return $client->getAcsResponse($request);
    }
}